﻿/// <summary>
/// 功能：
/// 作者：chao.jiang
/// 日期：2012/10/30 14:32:49
/// 备注：
/// 修改：
/// </summary>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WeifenLuo.WinFormsUI.Docking;
using System.IO;
using ICSharpCode.TextEditor.Document;
using System.Windows.Forms;

namespace DalGen
{
    public class DocForm : DockContent
    {
        private ICSharpCode.TextEditor.TextEditorControl textEditorControl1;

        public DocForm()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.textEditorControl1 = new ICSharpCode.TextEditor.TextEditorControl();
            this.SuspendLayout();
            // 
            // textEditorControl1
            // 
            this.textEditorControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.textEditorControl1.IsReadOnly = false;
            this.textEditorControl1.Location = new System.Drawing.Point(12, 12);
            this.textEditorControl1.Name = "textEditorControl1";
            this.textEditorControl1.Size = new System.Drawing.Size(698, 567);
            this.textEditorControl1.TabIndex = 0;
            this.textEditorControl1.Text = "textEditorControl1";
            // 
            // DocForm
            // 
            this.ClientSize = new System.Drawing.Size(722, 591);
            this.Controls.Add(this.textEditorControl1);
            this.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.Name = "DocForm";
            this.Load += new System.EventHandler(this.DocForm_Load);
            this.ResumeLayout(false);

        }

        public string FilePath { get; set; }

        private void DocForm_Load(object sender, EventArgs e)
        {
            StreamReader objReader = new StreamReader(FilePath);
            string sLine = objReader.ReadToEnd();
            objReader.Close();
            textEditorControl1.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy("C#");
            textEditorControl1.Document.TextContent = sLine;
            textEditorControl1.ShowLineNumbers = true;

        }

        protected override void OnClosed(EventArgs e)
        {
            DialogResult result = MessageBox.Show("是否保存?\r\n" + FilePath, "关闭文件", MessageBoxButtons.YesNoCancel);

            switch (result)
            {
                case DialogResult.Yes:
                    SaveFile();
                    base.OnClosed(e);
                    break;
                case DialogResult.No:
                    base.OnClosed(e);
                    break;
                case DialogResult.Cancel:
                    break;
            }
        }

        private void SaveFile()
        {
            FileStream fs = new FileStream(FilePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            try
            {
                sw.Write(textEditorControl1.Text);
                sw.Flush();;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                sw.Close();
                fs.Close();
            }
        }


    }
}
